DeleteReservoir Subroutine

private subroutine DeleteReservoir(list, elem)

delete a reservoir from the list

list points to the second element in the list

Arguments

Type IntentOptional Attributes Name
type(Reservoir), intent(inout), POINTER :: list

header of the list

type(Reservoir), intent(inout), POINTER :: elem

element to be removed


Variables

Type Visibility Attributes Name Initial
type(Reservoir), public, POINTER :: current
type(Reservoir), public, POINTER :: prev

Source Code

SUBROUTINE DeleteReservoir &
!
(list, elem)

IMPLICIT NONE

!Arguments with intent(inout):
TYPE(Reservoir), POINTER, INTENT (INOUT) :: list  !! header of the list
TYPE(Reservoir), POINTER, INTENT (INOUT) :: elem  !! element to be removed

!local declarations
TYPE(Reservoir), POINTER :: current  
TYPE(Reservoir), POINTER :: prev 


!---------------------------end of declarations--------------------------------

IF (ASSOCIATED (list, elem) ) THEN !elem is the header of the list
    list => elem % next !!list points to the second element in the list
    DEALLOCATE (elem)
ELSE !the element to be removed is in the middle of the list
    current => list
    prev => list
    
    DO WHILE ( ASSOCIATED (current) )
        IF ( ASSOCIATED ( current, elem) ) THEN
            prev % next => current % next
            DEALLOCATE ( current ) !is also elem
            EXIT
        END IF
        
        prev => current
        current => current % next
    END DO
      
END IF

RETURN
END SUBROUTINE DeleteReservoir